package com.zx.znav.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zx.znav.bean.Link;
import com.zx.znav.bean.Navigation;
import com.zx.znav.constant.Constants;
import com.zx.znav.controller.vo.LinkVO;
import com.zx.znav.error.BusinessException;
import com.zx.znav.error.EnumBusinessError;
import com.zx.znav.service.LinkService;
import com.zx.znav.service.NavigationService;
import com.zx.znav.service.TagService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 首页
 * @since 2023-07-18 14:50
 */
@Controller
public class IndexController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private LinkService linkService;
    @Autowired
    private TagService tagService;

    @RequestMapping("/")
    public String index(Model model){
        //初始选中第一个一级导航
        clickFirstNav(model, null);
        return "index";
    }

    /**
     * 设置模型数据
     * @param id 点击的一级导航列表id,为空选择所有一级导航中的第一个
     */
    private void clickFirstNav(Model model, Integer id){
        //查一级导航菜单列表
        List<Navigation> firstNavList = navigationService.listFirstNavigation();
        if(firstNavList.size() > 0){
            model.addAttribute(Constants.FIRST_NAVIGATION_KEY, firstNavList);
            Integer firstNavId;
            if(id == null){ //没传id选择第一个
                firstNavId = firstNavList.get(0).getId();
            }else{
                firstNavId = id;
            }
            //标记当期激活的一级导航
            model.addAttribute(Constants.FIRST_NAV_ID, firstNavId);
            //其下的所有二级导航
            List<Navigation> secondNavList = navigationService.listSecondNavByFirstNavId(firstNavId);
            model.addAttribute(Constants.SECOND_NAVIGATION_KEY, secondNavList);
            //第一个二级导航下的链接
            if(secondNavList.size() > 0){
                Integer secondNavId = secondNavList.get(0).getId();
                List<Link> linkList = linkService.listLinkByFirsNavIdAndSecondNavId(firstNavId, secondNavId);
                model.addAttribute(Constants.LinKS_KEY, linkList);
                //标记当期激活的二级导航
                model.addAttribute(Constants.Second_NAV_ID, secondNavId);
            }
        }
    }

    /**
     * 点击二级导航菜单，设置模型数据
     * @param firstNavId 一级导航id
     * @param secondNavId 二级导航id
     */
    private void clickSecondNav(Model model, Integer firstNavId, Integer secondNavId){
        //查一级导航菜单列表
        List<Navigation> firstNavList = navigationService.listFirstNavigation();
        if(firstNavList.size() > 0){
            model.addAttribute(Constants.FIRST_NAVIGATION_KEY, firstNavList);
            //标记当期激活的一级导航
            model.addAttribute(Constants.FIRST_NAV_ID, firstNavId);
            //其下的所有二级导航
            List<Navigation> secondNavList = navigationService.listSecondNavByFirstNavId(firstNavId);
            model.addAttribute(Constants.SECOND_NAVIGATION_KEY, secondNavList);
            //第一个二级导航下的链接
            if(secondNavList.size() > 0){
                List<Link> linkList = linkService.listLinkByFirsNavIdAndSecondNavId(firstNavId, secondNavId);
                model.addAttribute(Constants.LinKS_KEY, linkList);
                //标记当期激活的二级导航
                model.addAttribute(Constants.Second_NAV_ID, secondNavId);
            }
        }
    }


    @PostMapping("/nav")
    public String nav(Model model, Integer firstNavId, @RequestParam(required = false) Integer secondNavId){
        if(secondNavId == null){    //点击一级导航列表
            clickFirstNav(model, firstNavId);
        }else{  //点击二级导航列表
            clickSecondNav(model, firstNavId, secondNavId);
        }
        return  "index :: content";
    }

    @RequestMapping("/test")
    public String test() throws BusinessException {
        throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "测试异常");
    }

    /**
     * 加载最热标签/链接
     */
    @GetMapping("/footer/footData")
    public String footData(Model model){
        model.addAttribute("footLinks", linkService.topLink(3));
        model.addAttribute("footTags", tagService.listTopTag(3));
        return "fregments :: footData";
    }
}
